package org.example.authservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.transaction.Transactional;
import org.example.authservice.mapper.PermissionRoleMapper;
import org.example.authservice.pojo.PermissionRole;
import org.example.authservice.pojo.SavePermissionParm;
import org.example.authservice.service.PermissionRoleService;
import org.springframework.stereotype.Service;

/**
 * @author 顾翔宇
 * @since 2024/12/16 11：34
 * @version 1.0
 */
@Service
public class PermissionRoleServiceImpl extends ServiceImpl<PermissionRoleMapper, PermissionRole> implements PermissionRoleService {

    /**
     * 保存权限角色关系
     *
     * @param parm 权限角色关系参数
     */
    @Override
    @Transactional
    public void savePermissionRole(SavePermissionParm parm) {
        QueryWrapper<PermissionRole> query = new QueryWrapper<>();
        query.lambda().eq(PermissionRole::getRoleId,parm.getRoleId());
        this.baseMapper.delete(query);
        this.baseMapper.savePermissionRole(parm.getRoleId(),parm.getList());
    }

    /**
     * 根据权限ID删除权限角色关系
     *
     * @param permissionId 权限ID
     * @return 是否成功
     */
    @Override
    public boolean removeByPermissionId(Integer permissionId) {
        QueryWrapper<PermissionRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(PermissionRole::getPermissionId, permissionId);
        return remove(queryWrapper);
    }
}
